package com.zym.init.model.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zym.init.core.mybatis.type.SetTypeHandler;
import com.zym.init.core.utils.SpringUtils;
import com.zym.init.model.vo.PermissionVo;
import com.zym.init.model.vo.RoleVo;
import com.zym.init.service.PermissionService;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 角色表
 *
 * @TableName role
 */
@TableName(value = "role", autoResultMap = true)
@Data
public class RolePo implements Serializable {
    @Serial
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
    /**
     * ID
     */
    @TableId(type = IdType.AUTO)
    private Long roleId;
    /**
     * 角色名称
     */
    private String roleName;
    /**
     * 角色代码
     */
    private String roleCode;
    /**
     * 角色描述
     */
    private String roleDesc;
    /**
     * 权限列表
     */
    @TableField(typeHandler = SetTypeHandler.class)
    private Set<String> permissionList;
    /**
     * 是否为默认权限
     */
    private Boolean isDefault;
    /**
     * 状态：(1:启用[active] 2:禁用[inactive])
     */
    private String status;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 转Vo
     */
    public RoleVo toVo() {
        RoleVo vo = new RoleVo();
        vo.setRoleId(roleId);
        vo.setRoleName(roleName);
        vo.setRoleCode(roleCode);
        vo.setRoleDesc(roleDesc);
        vo.setStatus(status);
        vo.setIsDefault(isDefault);
        vo.setCreateTime(createTime);
        vo.setUpdateTime(updateTime);

        // 查询权限信息
        if (permissionList != null && !permissionList.isEmpty()) {
            PermissionService permissionService = SpringUtils.getBean(PermissionService.class);
            // 获取权限信息
            List<PermissionVo> permissionVos = permissionService.getByCodeList(permissionList.stream().toList());
            // 更新权限信息
            vo.setPermissionList(permissionVos);
        }
        
        return vo;
    }
}